/************************************************************************************/
/*  Program name CAPFAIMP_CAT_BOOK.sql                                        		*/
/*  Creater:  NIP Karen (PCCW - UniHub)                                         	*/
/*************   History   *************************                                */
/* 6 JAN, 2006   NIP Karen         First created                               		*/
/************************************************************************************/
SET SERVEROUTPUT ON SIZE 1000000;

DECLARE
	lv_Prog_ID		VARCHAR2(100)	:=	'CAPFAIMP_CAT_BOOK';

	lv_Scc_Count	NUMBER	:=	0;
	lv_Err_Count	NUMBER	:=	0;
	lv_Err_Msg		VARCHAR2(500)	:=	'No Error';

	CURSOR Cur_F IS
	SELECT
		*
	FROM
		CRST_FA_CATEGORY_BOOKS;
	

BEGIN
	UPDATE
		FA_CATEGORY_BOOKS
	SET	
		BOOK_TYPE_CODE = CAPBRIMP_MAPPING.RenameStr(SUBSTR(BOOK_TYPE_CODE,1,4), BOOK_TYPE_CODE)
	WHERE BOOK_TYPE_CODE IN
		(
		SELECT 
			BOOK_TYPE_CODE
		FROM 
			CRST_FA_CATEGORY_BOOKS
		);

	FOR C1 IN Cur_F LOOP
		BEGIN		
			INSERT INTO FA_CATEGORY_BOOKS
			VALUES
			(
			C1.CATEGORY_ID,
			C1.BOOK_TYPE_CODE,
			C1.LAST_UPDATE_DATE,
			CAPBRIMP_COMMON.GetUser(C1.LAST_UPDATED_NAME),
			CAPBRIMP_COMMON.GetUser(C1.CREATED_NAME),
			C1.CREATION_DATE,
			C1.LAST_UPDATE_LOGIN,
			C1.ASSET_CLEARING_ACCT,
			C1.ASSET_COST_ACCT,
			C1.ATTRIBUTE1,
			C1.ATTRIBUTE2,
			C1.ATTRIBUTE3,
			C1.ATTRIBUTE4,
			C1.ATTRIBUTE5,
			C1.ATTRIBUTE6,
			C1.ATTRIBUTE7,
			C1.ATTRIBUTE8,
			C1.ATTRIBUTE9,
			C1.ATTRIBUTE10,
			C1.ATTRIBUTE11,
			C1.ATTRIBUTE12,
			C1.ATTRIBUTE13,
			C1.ATTRIBUTE14,
			C1.ATTRIBUTE15,
			C1.ATTRIBUTE_CATEGORY_CODE,
			C1.CIP_CLEARING_ACCT,
			C1.CIP_COST_ACCT,
			C1.DEPRN_EXPENSE_ACCT,
			C1.DEPRN_RESERVE_ACCT,
			C1.REVAL_AMORTIZATION_ACCT,
			C1.REVAL_RESERVE_ACCT,
			CAPBRIMP_MAPPING.GetCCIDMapping(C1.ASSET_COST_ACCOUNT_CCID_D),
			CAPBRIMP_MAPPING.GetCCIDMapping(C1.ASSET_CLEARING_ACCOUNT_CCID_D),
			CAPBRIMP_MAPPING.GetCCIDMapping(C1.WIP_COST_ACCOUNT_CCID_D),
			CAPBRIMP_MAPPING.GetCCIDMapping(C1.WIP_CLEARING_ACCOUNT_CCID_D),
			CAPBRIMP_MAPPING.GetCCIDMapping(C1.RESERVE_ACCOUNT_CCID_D),
			CAPBRIMP_MAPPING.GetCCIDMapping(C1.REVAL_AMORT_ACCOUNT_CCID_D),
			CAPBRIMP_MAPPING.GetCCIDMapping(C1.REVAL_RESERVE_ACCOUNT_CCID_D),
			C1.LIFE_EXTENSION_CEILING,
			C1.LIFE_EXTENSION_FACTOR,
			C1.PERCENT_SALVAGE_VALUE,
			C1.GLOBAL_ATTRIBUTE1,
			C1.GLOBAL_ATTRIBUTE2,
			C1.GLOBAL_ATTRIBUTE3,
			C1.GLOBAL_ATTRIBUTE4,
			C1.GLOBAL_ATTRIBUTE5,
			C1.GLOBAL_ATTRIBUTE6,
			C1.GLOBAL_ATTRIBUTE7,
			C1.GLOBAL_ATTRIBUTE8,
			C1.GLOBAL_ATTRIBUTE9,
			C1.GLOBAL_ATTRIBUTE10,
			C1.GLOBAL_ATTRIBUTE11,
			C1.GLOBAL_ATTRIBUTE12,
			C1.GLOBAL_ATTRIBUTE13,
			C1.GLOBAL_ATTRIBUTE14,
			C1.GLOBAL_ATTRIBUTE15,
			C1.GLOBAL_ATTRIBUTE16,
			C1.GLOBAL_ATTRIBUTE17,
			C1.GLOBAL_ATTRIBUTE18,
			C1.GLOBAL_ATTRIBUTE19,
			C1.GLOBAL_ATTRIBUTE20,
			C1.GLOBAL_ATTRIBUTE_CATEGORY,
			C1.DEFAULT_GROUP_ASSET_ID,
			C1.BONUS_DEPRN_EXPENSE_ACCT,
			C1.BONUS_DEPRN_RESERVE_ACCT,
			CAPBRIMP_MAPPING.GetCCIDMapping(C1.BONUS_RESERVE_ACCT_CCID_D)
			);
		EXCEPTION
			WHEN OTHERS THEN
				DBMS_OUTPUT.Put_Line(SQLERRM);
				lv_Err_Count	:=	lv_Err_Count + 1;
		END;
		lv_Scc_Count	:=	lv_Scc_Count	+	1;
	END LOOP;
	
	IF	lv_Err_Count	>	0	THEN
		ROLLBACK;
	END IF;

	INSERT INTO CAPCNV_LOG(
		PROG_ID,
		PROCESS_DATE,
		RECORD_PROCESSED,
		RECORD_REJECTED,
		RECORD_EXTRACTED,
		ERR_MSG
	) VALUES (
		lv_Prog_ID,
		SYSDATE,
		lv_Scc_Count,
		lv_Err_Count,
		lv_Scc_Count-lv_Err_Count,
		lv_Err_Msg
	);
	
	COMMIT;
END;
/